// SPDX-License-Identifier: GPL-2.0

#include <linux/err.h>
#include "dim_tpm.h"
#include "dim_adapter.h"

static unsigned long dim_extend_type;

unsigned long dim_set_extend_type(unsigned long extend_type)
{
	dim_extend_type = 0;

	if (extend_type & DIM_EXTEND_TYPE_TPM2) {
		dim_extend_type |= DIM_EXTEND_TYPE_TPM2;
		dim_tpm_chip_init();
	}

	/* if there are some unsupported types, it will ret none zero */
	return extend_type ^ dim_extend_type;
}

void dim_reset_extend_type(void)
{
	if (dim_extend_type & DIM_EXTEND_TYPE_TPM2) {
		dim_extend_type ^= DIM_EXTEND_TYPE_TPM2;
		dim_tpm_chip_uninit();
	}
}

int dim_extend(char *digest, int len)
{
	int ret = 0;

	if (digest == NULL || len <= 0)
		return -EINVAL;

	if (dim_extend_type & DIM_EXTEND_TYPE_TPM2)
		ret = dim_tpm_pcr_extend(digest, len);

	return ret == -ENODEV ? 0 : ret;
}

/* it is used printed in measure log, only support pcr 12 currently */
int dim_get_extend_flag(void)
{
	return DIM_TPM_PCR;
}

